﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Medianamik.UI.Lock;
using Medianamik.Core.Configuration;
using System.Configuration;
using Medianamik.Core;

namespace Medianamik.UI.Web.UserControls
{
    public partial class LockInfoTooltip_ : System.Web.UI.UserControl
    {
        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUnlock_Click(object sender, EventArgs e)
        {
            LockManager.UnlockObject(ObjectId);

            this.RadTooltip.Visible = false;
            OnLockDeleted(EventArgs.Empty);
        }

        #region LockDeleted Event

        private static readonly object _lockDeletedEvent = new object();
        public event EventHandler LockDeleted
        {
            add
            {
                Events.AddHandler(_lockDeletedEvent, value);
            }
            remove
            {
                Events.RemoveHandler(_lockDeletedEvent, value);
            }
        }

        protected void OnLockDeleted(EventArgs e)
        {
            EventHandler handler = Events[_lockDeletedEvent] as EventHandler;
            if (handler != null)
            {
                handler(this, e);
            }

        }

        #endregion

        #endregion

        #region Properties

        public Guid ObjectId
        {
            get
            {
                return (Guid?)ViewState["ObjectId"] ?? Guid.Empty;
            }
            set
            {
                ViewState["ObjectId"] = value;
            }
        }

        public int Ultimatum
        {
            get
            {
                return (int?)ViewState["Ultimatum"] ?? (int)(ViewState["Ultimatum"] 
                    = ((MedianamikConfigSection)ConfigurationManager.
                    GetSection(ConfigurationSectionName.MEDIANAMIK)).UltimatumLock /*Default Value*/);
            }
            set
            {
                ViewState["Ultimatum"] = value;
            }
        }

        private LockInfo _lockInfo;
        public LockInfo LockInfo
        {
            get
            {
                return _lockInfo
                    ?? (_lockInfo = LockManager.GetInfo(ObjectId));
            }
        }

        #endregion

        #region Methods

        public void Show()
        {
            RadTooltip.Show();
            RadTooltip.Visible = true;
        }

        #endregion
    }
}